<!--
  * 生产订单与机器对应表
  *
  * @Author:    AKITA
  * @Date:      2025-04-19 12:46:38
  * @Copyright  AKITA
-->
<template>
  <a-drawer :title="form.lineNum ? '编辑' : '添加'" width="30%" :open="visibleFlag" @close="onClose" :maskClosable="false" :destroyOnClose="true">
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 3 }">
      <a-form-item label="物料编码" name="itemId">
        <a-input v-model:value="form.itemId" placeholder="行号" />
      </a-form-item>
      <a-form-item label="物料名称" name="itemName">
        <a-input v-model:value="form.itemName" placeholder="行号" />
      </a-form-item>
      <a-form-item label="规格" name="specs">
        <a-input v-model:value="form.specs" placeholder="行号" />
      </a-form-item>
      <a-form-item label="单位" name="unit">
        <a-input v-model:value="form.unit" placeholder="行号" />
      </a-form-item>
      <a-form-item label="数量" name="qty">
        <a-input-number style="width: 100%" v-model:value="form.qty" :min="0.0001" :step="0.0001" placeholder="行号" />
      </a-form-item>
      <a-form-item label="齐套" name="kitting">
        <a-checkbox  v-model:checked="form.kitting"></a-checkbox>
      </a-form-item>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-drawer>
</template>
<script setup>
  import { reactive, ref, nextTick } from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { mmordaApi } from '/@/api/business/erp/mmord-api';
  import { smartSentry } from '/@/lib/smart-sentry';

  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList']);

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
    id: undefined, //电键
    lineNum: undefined, //行号
    itemId: undefined,
    itemName: undefined,
    specs: undefined,
    unit: undefined,
    qty: undefined,
  };

  let form = reactive({ ...formDefault });

  const rules = {
    docEntry: [{ required: true, message: '电键 必填' }],
    lineNum: [{ required: true, message: '行号 必填' }],
  };

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save();
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show();
    try {
      if (form.lineNum) {
        await mmordaApi.update(form);
      } else {
        await mmordaApi.add(form);
      }
      message.success('操作成功');
      emits('reloadList');
      onClose();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  defineExpose({
    show,
  });
</script>
